#property copyright ""
#property link      "mingxinhe@gmail.com"

#property indicator_separate_window
#property indicator_maximum 10
#property indicator_minimum -10

extern color  UpColor = Green; 
extern color  DownColor = Red;

int digitFactor = 1;

double lastPrice, topBoxPrice, bottomBoxPrice, lastTopBoxPrice, lastBottomBoxPrice;
datetime lastTime;
int windowsIdx;

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
void init() 
{
   DeleteObjects();
      
   if(Bid<10 && Digits==5) { digitFactor=10;}
   if(Bid<10 && Digits==4) { digitFactor= 1;}

   if(Bid>10 && Digits==3) { digitFactor=10;}
   if(Bid>10 && Digits==2) { digitFactor= 1;} 
      
   IndicatorShortName("Boxes");   
   
   lastTime = Time[0];
}

//+------------------------------------------------------------------+
//| Custor indicator deinitialization function                       |
//+------------------------------------------------------------------+
void deinit() 
{
    Comment("");
    DeleteObjects();
    ObjectsDeleteAll(0, OBJ_TEXT);
    return(0);
}

//+------------------------------------------------------------------+
//| Remove all Rectangles                                            |
//+------------------------------------------------------------------+
void DeleteObjects() 
{
    ObjectsDeleteAll(0,OBJ_RECTANGLE);     
    return(0); 
}

void start() 
{   
    int tempInt = Close[0]/Point - MathFloor(Close[0]/Point/(100*digitFactor))*(100*digitFactor);

    int botInt = MathFloor(tempInt/(12.5*digitFactor));
    int topInt = MathCeil(tempInt/(12.5*digitFactor));
    
    //if (botInt % 2 == 0) botInt = botInt - 1;
    //if (topInt % 2 == 0) topInt = topInt + 1;
    
    windowsIdx = WindowFind("Boxes");        
    
    topBoxPrice = (MathFloor(Close[0]/Point/(100*digitFactor))*(100*digitFactor) + topInt*12.5*digitFactor)*Point;
    bottomBoxPrice = (MathFloor(Close[0]/Point/(100*digitFactor))*(100*digitFactor) + botInt*12.5*digitFactor)*Point;
    
    if (topBoxPrice != lastTopBoxPrice && bottomBoxPrice != lastBottomBoxPrice)
    {
        if (Low[1] > lastBottomBoxPrice && Close[0] <= lastBottomBoxPrice && lastBottomBoxPrice > 0 && Time[0] > lastTime)
        {
            string id = TimeCurrent();
            ObjectCreate(id, OBJ_RECTANGLE, windowsIdx, 0, 0, 0, 0);
            ObjectSet(id, OBJPROP_TIME1 , lastTime);
            ObjectSet(id, OBJPROP_TIME2 , Time[0]);
            ObjectSet(id, OBJPROP_PRICE1, lastPrice);  
            ObjectSet(id, OBJPROP_PRICE2, lastPrice - 1);
            ObjectSet(id, OBJPROP_STYLE, STYLE_SOLID);      
            ObjectSet(id, OBJPROP_BACK, True);
            ObjectSet(id, OBJPROP_COLOR, DownColor);
            lastPrice -= 1;
            lastTime = Time[0];
            Print("go down touch ", DoubleToStr(lastBottomBoxPrice, Digits), " at ", TimeToStr(lastTime));
        }  
   
        if (High[1] < lastTopBoxPrice && Close[0] >= lastTopBoxPrice && lastTopBoxPrice > 0 && Time[0] > lastTime)
        {
            id = TimeCurrent();
            ObjectCreate(id, OBJ_RECTANGLE, windowsIdx, 0, 0, 0, 0);
            ObjectSet(id, OBJPROP_TIME1 , lastTime);
            ObjectSet(id, OBJPROP_TIME2 , Time[0]);
            ObjectSet(id, OBJPROP_PRICE1, lastPrice);  
            ObjectSet(id, OBJPROP_PRICE2, lastPrice + 1);
            ObjectSet(id, OBJPROP_STYLE, STYLE_SOLID);      
            ObjectSet(id, OBJPROP_BACK, True);
            ObjectSet(id, OBJPROP_COLOR, UpColor);
            lastPrice += 1;
            lastTime = Time[0];
            Print("go up touch ", DoubleToStr(lastTopBoxPrice, Digits), " at ", TimeToStr(lastTime));
        } 
        
        lastTopBoxPrice = topBoxPrice;  
        lastBottomBoxPrice = bottomBoxPrice;
    }
    
    Comment("topBoxPrice :", DoubleToStr(topBoxPrice, Digits), " bottomBoxPrice:", DoubleToStr(bottomBoxPrice, Digits), " lastTime:", TimeToStr(lastTime), " lastPrice:",lastPrice);  
}